/*
 * $URL: http://dnsprobe.googlecode.com/svn/trunk/dnsprobe/src/dnsprobe/gui/renderer/IpTableCellRenderer.java $
 * 
 * Copyright (c) 2009, Anonymous
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Anonymous nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY ANONYMOUS ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL ANONYMOUS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

package dnsprobe.gui.renderer;

import static dnsprobe.Constants.GUI_MAIN_TABLE_BACKGROUND_COLOR_EQUAL;
import static dnsprobe.Constants.GUI_MAIN_TABLE_BACKGROUND_COLOR_NOT_EQUAL;
import static dnsprobe.Constants.GUI_MAIN_TABLE_BACKGROUND_COLOR_NO_RESULT;
import static dnsprobe.Constants.GUI_MAIN_TABLE_FOREGROUND_COLOR_EQUAL;
import static dnsprobe.Constants.GUI_MAIN_TABLE_FOREGROUND_COLOR_NOT_EQUAL;
import static dnsprobe.Constants.GUI_MAIN_TABLE_FOREGROUND_COLOR_NO_RESULT;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import dnsprobe.Application;
import dnsprobe.data.Hostname;


/**
 * Aendert das Erscheinungsbild der IpTable
 * 
 * @author BerndBernhardson
 * @author anon004
 * @author snoop
 * @version $Id: IpTableCellRenderer.java 164 2009-07-08 01:42:55Z bernhardson@gmx.net $
 *
 */
public class IpTableCellRenderer extends DefaultTableCellRenderer
{
	private static final long serialVersionUID = 1L;

	/**
	 * Faerbt die Tabellenzellen ein
	 * 
	 * <p>Folgende Farben gelten:</p>
	 * 
	 * <pre>
	 * Status                  | Schriftfarbe | Hintergrundfarbe
	 * ------------------------+--------------+------------------
	 * Kein Ergebnis vorhanden | Schwarz      | Weiss
	 * Ergebnisse gleich       | Schwarz      | Gruen
	 * Ergebnisse gleich       | Weiss        | Rot
	 * </pre>
	 * 
     * @param table  the <code>JTable</code>
     * @param value  the value to assign to the cell at <code>[row, column]</code>
     * @param isSelected true if cell is selected
     * @param hasFocus true if cell has focus
     * @param row  the row of the cell to render
     * @param column the column of the cell to render
     * @return the default table cell renderer
	 */
	@Override
	public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column)
	{
		final Component comp;
		final Hostname  hostname;
		
		comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		
		hostname = Application.getData().getHostnameList().get(row);
		
		if(isSelected)
		{
			comp.setForeground(table.getSelectionForeground());
			comp.setBackground(table.getSelectionBackground());
		}
		else if(hostname.hasResult())
		{
			comp.setForeground(hostname.hasDifferences() ? GUI_MAIN_TABLE_FOREGROUND_COLOR_NOT_EQUAL : GUI_MAIN_TABLE_FOREGROUND_COLOR_EQUAL);
			comp.setBackground(hostname.hasDifferences() ? GUI_MAIN_TABLE_BACKGROUND_COLOR_NOT_EQUAL : GUI_MAIN_TABLE_BACKGROUND_COLOR_EQUAL);
		}
		else
		{
			comp.setForeground(GUI_MAIN_TABLE_FOREGROUND_COLOR_NO_RESULT);
			comp.setBackground(GUI_MAIN_TABLE_BACKGROUND_COLOR_NO_RESULT);
		}
		
		return comp;
	}
}
